<?php $db = new Mypdo(); ?><?php


if (!empty($_SESSION['login'])&&!empty($_SESSION['email'])){

if(!empty($_GET['supprid'])&&!empty($_GET['id'])){


    if($_GET['supprid'] == $_GET['id']){
        $persManager = new PersonneManager($db);
        $locataireManager = new LocataireManager($db);
        $occManager = new OccupeManager($db);
        $loyerManager = new LoyerManager($db);

        $loyerManager->delLoyerByIdLoca($_GET['supprid']);
        $persManager->delPersByIdLoca($_GET['supprid']);
        $occManager->delOccuByIdLoca($_GET['supprid']);


        $locataireManager->delLoca($_GET['supprid']);

        header('Location: index.php?page=108');
    }
}else if(!empty($_GET['id'])&&!empty($_GET['idp'])){

    $partManager = new PartageManager($db);
    $partManager->delPartByIdPersLoca($_GET['idp'],$_GET['id']);
    header('Location: index.php?page=114&id='. $_GET['id']);


}else if(!empty($_GET['id'])&&!empty($_GET['idl'])){

    $occManager = new OccupeManager($db);
    $occManager->delOccuByIdLocLoca($_GET['idl'],$_GET['id']);
    header('Location: index.php?page=114&id='. $_GET['id']);


}else{
    if(empty($_POST)&&!empty($_GET['id'])){
        $locaManager = new LocataireManager($db);
        $locManager = new LocationManager($db);
        $persManager = new PersonneManager($db);
        $partManager = new PartageManager($db);

        $loca = $locaManager->getLoca($_GET['id']);

        $listeLoc = $locManager->getList();
        ?>

        <h1>Détail d'un locataire</h1>

        <form method="post" action="#">

            <p>




            <label for="date_bail_deb_loca">Date de début de bail :</label><input type="date" name="date_bail_deb_loca" id="date_bail_deb_loca"  required="required" value="<?php echo $loca->getDateBailDebLoca(); ?>"  /><br />
            <label for="date_bail_fin_loca">Date de fin de bail :</label><input type="date" name="date_bail_fin_loca" id="date_bail_fin_loca" required="required" value="<?php echo $loca->getDateBailFinLoca(); ?>"  /><br />

            <label for="date_bail_rev_loca">Date de revision de bail :</label><input type="date" name="date_bail_rev_loca" id="date_bail_rev_loca"  required="required" value="<?php echo $loca->getDateBailRevLoca(); ?>" /><br />

            </p>


            <p>
            <h3>Paiement</h3>
            <?php
            $paiManager = new PaiementManager($db);
            $listePai = $paiManager->getList(); ?>
            <label for="id_pai">Mode de paiement:</label><select name="id_pai" id="id_pai" required="required">
                <option></option>
                <?php foreach($listePai as $key=>$value){
                    ?><option value="<?php echo $value->getIdPai(); ?>"<?php echo $loca->getIdPai() == $value->getIdPai()?'selected':''; ?>><?php echo $value->getLibellePai(); ?></option><?php echo "\n";
                } ?>


            </select>
            </p>

            <p><input type="reset" value="Annuler" /><input type="submit" value="Modifier" />
                <input id="suppr" name="suppr" type="button" onclick="document.location.href='index.php?page=114&id=<?php echo $_GET['id']; ?>&supprid=<?php echo $loca->getIdLoca(); ?>'" value="Supprimer" />
            </p>
        </form>
  <?php
        /*
        *Personne qui loue
        */


        $listePers = array();
        $listid = $partManager->getIdPersByIdLoca($loca->getIdLoca());
        foreach ($listid as $value) {
                $listePers[] = $persManager->getPers($value['id_pers']);
        }

        if(!empty($listePers)){
        ?>

        <table class="tableau">
            <tr>
                <th>
                    Nom
                </th>
                <th>
                    Prénom
                </th>
                <th>
                    Détail
                </th>
                <th>
                    Supprimer
                </th>
            </tr>

            <?php foreach ($listePers as $value) {
                ?>

            <tr>
                <td>
                    <?php echo $value->getNomPers(); ?>
                </td>
                <td>
                    <?php echo $value->getPrenomPers(); ?>
                </td>
                <td>
                    <input id="detail" name="detail" type="button" onclick="document.location.href='index.php?page=117&id=<?php echo $value->getIdPers(); ?>'" />
                </td>
                <td>
                    <a  href="index.php?page=114&id=<?php echo $_GET['id'];  ?>&idp=<?php echo $value->getIdPers(); ?>">
                        <img id="ico" src="image/erreur.png" alt="Supprimer">
                    </a>
                </td>
            </tr>

            <?php
            }
            ?>

        </table>



    <?php
        }

    ?>
<form action="#" method="post">
    <label for="pers"></label><select id="pers" name="pers">
        <option selected value="0" >Nouvel Personne</option>
        <?php
        $liste = $persManager->getListNouv($_GET['id']);
        foreach ($liste as $value) {
            ?>

        <option value="<?php echo $value->getIdPers(); ?>"><?php echo $value->getNomPers().' '.$value->getPrenomPers() ?></option><?php echo "\n";  ?>

        <?php
        }
        ?>

    </select>
    <input type="submit" name="addPers" id="addPers" value="Ajouter une personne" />
</form>
<?php
/*
 *Location
 */
?>
        <?php
        $occManager = new OccupeManager($db);
        $listeLoc = array();
        $listid = $occManager->getIdLocByIdLoca($loca->getIdLoca());
        foreach ($listid as $value) {
            $listeLoc[] = $locManager->getLoc($value['id_loc']);
        }

        if(!empty($listeLoc)){
            ?>

            <table class="tableau">
                <tr>
                    <th>
                        Location
                    </th>
                    <th>
                        Détail
                    </th>
                    <th>
                        Supprimer
                    </th>
                </tr>

                <?php foreach ($listeLoc as $value) {
                    ?>

                    <tr>
                        <td>
                            <?php echo $value->getNomLoc(); ?>
                        </td>
                        <td><input id="detail" name="detail" type="button" onclick="document.location.href='index.php?page=113&id=<?php echo $value->getIdLoc(); ?>'" /></td>
                        <td>
                            <a  href="index.php?page=114&id=<?php echo $_GET['id'];  ?>&idl=<?php echo $value->getIdLoc(); ?>">
                                <img id="ico" src="image/erreur.png" alt="Supprimer">
                            </a>
                        </td>
                    </tr>

                <?php
                }
                ?>

            </table>



        <?php
        }

        ?>
        <form action="#" method="post">
            <label for="loc"></label><select id="loc" name="loc">
                <option value="0">Selectionner...</option>
                <?php
                $liste = $locManager->getListNouv($_GET['id']);
                foreach ($liste as $value) {
                    ?>

                    <option value="<?php echo $value->getIdLoc(); ?>"><?php echo $value->getNomLoc() ?></option><?php echo "\n";  ?>

                <?php
                }
                ?>

            </select>
            <input type="submit" name="addLoc" id="addLoc" value="Ajouter une location" />
        </form>

    <?php


    }else if(isset($_POST['pers'])){
        if(empty($_POST['pers'])){
         header('Location: index.php?page=116&id='.$_GET['id']);
        }else{
        $partManager = new PartageManager($db);
        $partManager->add(array('id_loca'=> $_GET['id'],'id_pers'=>$_POST['pers']));
        header('Location: #');
        }
    }else if(isset($_POST['loc'])){
        if(!empty($_POST['loc'])||$_POST['loc'] != 'Selectionner...'){
            try{
            $occManager = new OccupeManager($db);
            $locaManager = new LocataireManager($db);
            $loca = $locaManager->getLoca($_GET['id']);

            LocataireRegle::dispoLoca($_POST['loc'],$loca,$db);

            $occManager->add(array('id_loca'=> $_GET['id'],'id_loc'=>$_POST['loc']));
            header('Location: #');

            }catch (ImmolocException $e){
                ?>

                <div class="erreurSaisi"><p>
                        <?php
                        echo $e->getMessage();
                        //header('Location: #');

                        ?>
                    </p>
                </div>

            <?php
            }
        }

    }else{
    try{
        $locaManager = new LocataireManager($db);
        $locaN = new Locataire($_POST);
        $locaN->setIdLoca($_GET['id']);

        $loca = $locaManager->getLoca($_GET['id']);

        $locManager = new LocationManager($db);
        foreach ($locManager->getLocByIdLoca($_GET['id']) as $value) {
            LocataireRegle::dispoLoca($value->getIdLoc(),$locaN,$db);
        }

        $locaManager->update($locaN);
        header('Location: #');
    }catch (ImmolocException $e){
        ?>

        <div class="erreurSaisi"><p>
                <?php
                echo $e->getMessage();
                //header('Location: #');

                ?>
            </p>
        </div>

    <?php

    }
    }
}
}else{
    header('Location: index.php');
}